<template>
  <el-form ref="formRef" v-loading="formLoading" :model="formData" :rules="formRules" label-width="200px" :disabled="disabled">
    <el-row>
      <el-col :span="12">
        <el-form-item label="模板名称" prop="templateName">
          <el-input v-model="formData.templateName" clearable placeholder="请输入模板名称" />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="模板ID" prop="templateId">
          <el-input v-model="formData.templateId" clearable placeholder="请输入模板ID" />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="消息是否带连接" prop="isLink">
          <el-select v-model="formData.isLink" placeholder="请选择" clearable>
            <el-option v-for="item in isOption" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="消息抬头" prop="messageHeader">
          <el-input v-model="formData.messageHeader" clearable placeholder="请输入消息抬头" />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="纯文本消息" prop="messageText">
          <el-input v-model="formData.messageText" clearable placeholder="请输入纯文本消息" />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="详版消息" prop="messageInfo">
          <el-input v-model="formData.messageInfo" clearable placeholder="请输入详版消息" />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="H5链接" prop="h5Url" v-if="formData.isLink == '1'">
          <el-input v-model="formData.h5Url" clearable placeholder="请输入H5链接" />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="是否长时间展示" prop="isLongTimeDisplay">
          <el-select v-model="formData.isLongTimeDisplay" placeholder="请选择" clearable>
            <el-option v-for="item in isOption" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="参数映射" prop="parpaMapping">
          <el-input v-model="formData.parpaMapping" maxlength="100" placeholder="请输入参数映射" show-word-limit
            type="textarea" />
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
  <el-col :span="24" style="text-align: center;">
    <el-button :disabled="formLoading" type="primary" @click="submitForm" v-if="formType != '查看'">确 定</el-button>
    <el-button @click="setClose">取 消</el-button>
  </el-col>
</template>
<script lang="ts" setup>
import * as messageTemplateApi from '@/api/infoArrive/arriveNotice/messageTemplate/index'

defineOptions({ name: 'MessageTemplateForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const disabled = ref(false)
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: null,
  templateName: '',
  resLevelName: '',
  templateId: '',
  isLink: '1',
  messageHeader: '',
  messageText: '',
  messageInfo: '',
  h5Url: '',
  isLongTimeDisplay: undefined,
  parpaMapping: '',
})
const isOption = ref([
  { label: '否', value: '0' }, { label: '是', value: '1' }
])

const formRules = reactive({
  templateName: [{ required: true, message: '模板名称不能为空', trigger: 'blur' }],
  templateId: [{ required: true, message: '模板ID不能为空', trigger: 'blur' }],
  isLink: [{ required: true, message: '请选择', trigger: 'change' }],
  messageHeader: [{ required: true, message: '消息抬头不能为空', trigger: 'blur' }],
  isLongTimeDisplay: [{ required: true, message: '请选择', trigger: 'change' }],
  isVoiceNotice: [{ required: true, message: '请选择', trigger: 'change' }],
  parpaMapping: [{ required: true, message: '参数映射不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = type
  formType.value = type
  disabled.value = (type === '查看')
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await messageTemplateApi.getTemplate(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success', 'setClose']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    if (formType.value === '新增') {
      await messageTemplateApi.saveTemplate({ ...formData.value })
      message.success(t('common.createSuccess'))
    } else {
      await messageTemplateApi.saveTemplate({ ...formData.value })
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
    setClose()
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: null,
    templateName: '',
    resLevelName: '',
    templateId: '',
    isLink: '1',
    messageHeader: '',
    messageText: '',
    messageInfo: '',
    h5Url: '',
    isLongTimeDisplay: undefined,
    parpaMapping: '',
  }
  formRef.value?.resetFields()
}
const setClose = () => {
  dialogVisible.value = false
  emit('setClose')
}
</script>
